﻿using System;

namespace Xfs
{
	public abstract class XfsAMActorLocationHandler<E, Message>: IXfsMActorHandler where E: XfsEntity where Message : class, IXfsActorLocationMessage
	{
		protected abstract XfsTask Run(E entity, Message message);

		public async XfsTask Handle(XfsSession session, XfsEntity entity, object actorMessage)
		{
			Message msg = actorMessage as Message;
			if (msg == null)
			{
				//Log.Error($"消息类型转换错误: {actorMessage.GetType().FullName} to {typeof (Message).Name}");
				return;
			}
			E e = entity as E;
			if (e == null)
			{
				//Log.Error($"Actor类型转换错误: {entity.GetType().Name} to {typeof(E).Name}");
				return;
			}
			
			XfsActorResponse actorResponse = new XfsActorResponse();
			actorResponse.RpcId = msg.RpcId;
			session.Reply(actorResponse);

			try
			{
				await this.Run(e, msg);
			}
			catch (Exception exception)
			{
				//Log.Error(exception);
			}
		}

		public Type GetMessageType()
		{
			return typeof (Message);
		}
	}
}